package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description
 * User:华为
 * Date:2024/10/18
 * Time:21:10
 */
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        int currentsize = bookList.getUsedSize();
        if (currentsize == bookList.getBooks().length){
            System.out.println("书架已满，不能存放！");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = scanner.nextLine();

        System.out.println("请输入作者");
        String author = scanner.nextLine();

        System.out.println("请输入书的类型");
        String type = scanner.nextLine();

        System.out.println("请输入书的价格");
        int price = scanner.nextInt();

        Book book = new Book(name,author,price,type);

        //若已有这本书则不进行添加

        for (int i = 0; i < currentsize; i++) {
            Book book1 = bookList.getBook(i);
            if (book1.getName().equals(name)){
                System.out.println("已有这本书，不进行添加！");
                return;
            }
        }
        bookList.setBooks(currentsize, book);
        bookList.setUsedSize(currentsize + 1);
        System.out.println("添加成功");



        //若书架满了则不进行添加
    }
}
